/** FASSIT (Forms of Selective Attention in Intelligent Transportation Systems)
 * Computational Creativity and Digital Media
 * Cognitive and Media Systems Group
 * Centre for Informatics and Systems of the University of Coimbra (CISUC)
 *
 * Copyright (c) 2010-2013 University of Coimbra, Portugal
 * All rights reserved.
 */
package functionsVariables;

import java.util.Calendar;
import java.text.SimpleDateFormat;

/**
 * ----------------------------------------------------------------------------
 * This class provides all the functions needed to work with the Calender in
 * Java
 * ----------------------------------------------------------------------------
 * 
 * @author Hernani Costa
 * @version 0.1 date: 22/10/2012
 */

public class CalendarFunctions {

	private SimpleDateFormat formatter;
	private int[] strNumberDay;
	private String[] strDay;

	public CalendarFunctions() {
		this.formatter = new SimpleDateFormat(
				"E yyyy.MM.dd 'at' hh:mm:ss a zzz");
		this.strNumberDay = Constants.getStrNumberDays();
		this.strDay = Constants.getStrDays();
	}

	public SimpleDateFormat getFormatter() {
		return formatter;
	}

	/**
	 * ------------------------------------------------------------------------
	 * This function ranges the actual time to the first week in January 2012,
	 * in order to range our experimentation in one week
	 * ------------------------------------------------------------------------
	 * 
	 * @param calendar
	 *            - current calendar
	 * @return a new calendar in the one week range
	 */
	public Calendar rangeCalendar(Calendar calendar) {
		/*
		 * System.err.println(calendar.get(Calendar.DAY_OF_WEEK));
		 * System.err.println(strNumberDay[calendar.get(Calendar.DAY_OF_WEEK)]);
		 * System.err.println(strNumberDay[calendar.get(Calendar.DAY_OF_WEEK) -
		 * 1]);
		 */
		int day = strNumberDay[calendar.get(Calendar.DAY_OF_WEEK) - 1];
		calendar.set(Calendar.DAY_OF_MONTH, day);
		calendar.set(Calendar.YEAR, 2012);
		calendar.set(Calendar.MONTH, 0);

		return calendar;
	}

	/**
	 * ------------------------------------------------------------------------
	 * Convert the calendar to a string that is used in the Database
	 * ------------------------------------------------------------------------
	 * 
	 * @param calendar
	 *            - calendar
	 * @return - string calendar
	 */
	public String convertTo_DBTIME(Calendar calendar) {

		String hour = calendar.get(Calendar.HOUR_OF_DAY) + "";
		if (hour.length() == 1) {
			hour = "0" + hour;
		}

		String minute = calendar.get(Calendar.MINUTE) + "";
		if (minute.length() == 1) {
			minute = "0" + minute;
		}

		return calendar.get(Calendar.YEAR) + "-"
				+ (calendar.get(Calendar.MONTH) + 1) + "-"
				+ calendar.get(Calendar.DAY_OF_MONTH) + " " + hour + ":"
				+ minute;
	}

	/**
	 * ------------------------------------------------------------------------
	 * Date Arithmetic function. Subtracts two dates.
	 * ------------------------------------------------------------------------
	 * 
	 * @param previous
	 *            - previous time
	 * @param now
	 *            - current time
	 * @return the difference in minutes
	 */
	public int doDifferenceInMinutes(Calendar previous, Calendar now) {
		long diff = now.getTimeInMillis() - previous.getTimeInMillis();
		return (int) (diff / (1000L * 60L));
	}

	/**
	 * Date Arithmetic function. Subtracts two dates.
	 * 
	 * @param previous
	 *            - previous time
	 * @param now
	 *            - current time
	 * @return the difference in seconds
	 */
	public int doDifferenceInSeconds(Calendar previous, Calendar now) {
		long diff = now.getTimeInMillis() - previous.getTimeInMillis();
		return (int) (diff / (1000L));
	}

	/**
	 * ------------------------------------------------------------------------
	 * Date Arithmetic function. Adds the specified (signed) amount of minutes
	 * to the given time field, based on the calendar's rules.
	 * ------------------------------------------------------------------------
	 * 
	 * @param previous
	 *            - previous time
	 * @param minutes
	 *            - minutes to be added
	 * @return the previous time plus the minutes passed through the parameters
	 */
	public Calendar doAddMinutes(Calendar previous, int minutes) {
		Calendar working;
		working = (Calendar) previous.clone();
		working.add(Calendar.MINUTE, +minutes);
		return working;
	}

	/**
	 * ------------------------------------------------------------------------
	 * Date Arithmetic function. Subtracts the specified (signed) amount of
	 * minutes to the given time field, based on the calendar's rules.
	 * ------------------------------------------------------------------------
	 * 
	 * @param previous
	 *            - previous time
	 * @param minutes
	 *            - minutes to be added
	 * @return the previous time menus the minutes passed through the parameters
	 */
	public Calendar doSubtractMinutes(Calendar previous, int minutes) {
		Calendar working;
		working = (Calendar) previous.clone();
		working.add(Calendar.MINUTE, -minutes);
		return working;
	}

	/**
	 * ------------------------------------------------------------------------
	 * Date Arithmetic function. Adds the specified (signed) amount of seconds
	 * to the given time field, based on the calendar's rules.
	 * ------------------------------------------------------------------------
	 * 
	 * @param previous
	 *            - previous time
	 * @param seconds
	 *            - seconds to be added
	 * @return the previous time plus the seconds passed through the parameters
	 */
	public Calendar doAddSeconds(Calendar previous, int seconds) {
		Calendar working;
		working = (Calendar) previous.clone();
		working.add(Calendar.SECOND, +seconds);
		return working;
	}

	public String timeToString(Calendar cal) {
		String time = null;

		time = "year: " + cal.get(Calendar.YEAR) + " \nmonth: "
				+ (cal.get(Calendar.MONTH) + 1) + " \nday: "
				+ cal.get(Calendar.DAY_OF_MONTH) + " \nnWeekday: "
				+ cal.get(Calendar.DAY_OF_WEEK) + " \nweekday: "
				+ strDay[cal.get(Calendar.DAY_OF_WEEK) - 1] + " \nhour:"
				+ cal.get(Calendar.HOUR_OF_DAY) + " \nmin:"
				+ cal.get(Calendar.MINUTE) + "\n";
		return time;

	}

	/**
	 * ------------------------------------------------------------------------
	 * Converts the string returned from the database (String) to a Calendar
	 * ------------------------------------------------------------------------
	 * 
	 * @param time
	 *            - string in the shape: (year-month-day hour:minute)
	 * @return Calendar
	 */
	public Calendar dbTime_to_Calendar(String time) {
		Calendar c = Calendar.getInstance();
		String[] ti = time.split(" ");
		String[] data = ti[0].trim().split("-");
		String[] t = ti[1].trim().split(":");
		int year = Integer.parseInt(data[0].trim());
		int month = Integer.parseInt(data[1].trim());
		int day = Integer.parseInt(data[2].trim());
		int hour = Integer.parseInt(t[0].trim());
		int minute = Integer.parseInt(t[1].trim());
		c.set(year, month, day, hour, minute);
		return c;
	}

	/**
	 * ------------------------------------------------------------------------
	 * Used to provide an example that exercises the functionality of the class.
	 * ------------------------------------------------------------------------
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		CalendarFunctions ce = new CalendarFunctions();

		Calendar previous = Calendar.getInstance();
		Calendar now = ce.doAddMinutes(previous, 65);

		System.out.println(ce.doDifferenceInMinutes(previous, now));
		System.out.println(ce.doDifferenceInSeconds(previous, now));

		System.out.println(ce.getFormatter().format(
				ce.doAddMinutes(previous, 5).getTime()));

		Calendar c = Calendar.getInstance();
		// Displays the current calendar
		System.out.println("Calendar before Setting is " + c.getTime());
		Calendar working;
		working = (Calendar) c.clone();
		System.out.println("Previous Calendar Clone: " + working.getTime());

		// Sets the values for the calendar fields YEAR, MONTH,and DAY_OF_MONTH.

		c.set(2012, 04, 27, 10, 23);
		// Displays the calendar after setting
		System.out.println("Calendar after Setting is " + c.getTime());

	}
}
